<!DOCTYPE html>
<%@ page import="java.util.*,rubix.model.*,rubix.controller.*,java.text.*,rubix.model.Supplier" %>
<%@include file ="masterPage.jsp" %>

<%
    String poId = (String)session.getAttribute("poID");
    String anotherPoId = request.getParameter("poID");
%>

<html>
    <head>
        <title>KMP Fast East Pte Ltd - ERP Application</title>
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="page-wrapper">
    <%
    if(poId != null) {
        Po po = PoManager.retrievePo(poId);
        String supplierId = po.getSupplierId();
        Supplier supplier = SupplierManager.retrieveSupplier(supplierId);
        String purchaseDate = po.getPurchaseDate();
        String day = purchaseDate.substring(0,2);
        String month = purchaseDate.substring(2,4);
        String year = purchaseDate.substring(4);
        String formatPurchaseDate = day + "/" + month + "/" + year;
        String partPrice = po.getPartPrice();
        double tempPrice = 0;
        try {
            tempPrice = Double.parseDouble(partPrice);
        } catch(NumberFormatException e) { } 
        DecimalFormat df2 = new DecimalFormat("##.00");
        double tempAmount = po.getOnOrderQuantity() * tempPrice;
        double totalAmount = Double.valueOf(df2.format(tempAmount));
        %>
        
            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">    
                        <form name="podetailsform" method="get" action="poController">
                        <h1 class="panel-title"><i class="icon-file-text-alt"></i> Purchase Order <%=poId%></h1><br>
                        <a href="poController"><button type="button" name="home" class="btn btn-success"><i class="icon-edit"></i> Home</button></a>
                        <a href="#"><button type="button" class="btn btn-default"><i class="icon-print"></i> Print</button></a>
                        <a href="#"><button type="button" class="btn btn-default"><i class="icon-mail-reply"></i> Email</button></a>
                        <a href="updatePurchaseOrder.jsp?poID=<%=poId%>"><button type="button" class="btn btn-danger"><i class="icon-edit"></i> Edit Order</button></a>
                        </form>
                    </div>
                    
                   
                    <div class="panel-body"> 
                        <div class="table-responsive">
                            <table class="table table-condensed table-hover table-striped tablesorter">                                
                                <thead>
                                    <h1>Purchase Order: <%=poId%></h1>
                                    <tr>
                                        <th>Supplier</th>
                                        <th>Date </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><address><strong><%=supplier.getSupplierId()%></strong><br>
                                            <%=supplier.getSupplierDescription()%><br>
                                            <%=supplier.getSupplierLocation()%><br></td>
                                        <td><%=formatPurchaseDate%></address></td>
                                    </tr>
                                    <tr>
                                </tbody>
                            </table>
                            <table class="table table-bordered table-hover table-striped tablesorter">
                                <thead  >
                                    <tr class="success">
                                        <th>No</th>
                                        <th>Item ID</th>
                                        <th>Part ID</th>
                                        <th>Quantity</th>
                                        <th>Unit Price</th>
                                        <th>Sub-Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>1</td>
                                        <td><%=po.getItemId()%></td>
                                        <td><%=po.getPartId()%></td>
                                        <td><%=po.getOnOrderQuantity()%></td>
                                        <td><%=po.getPartPrice()%></td>
                                        <td><%=totalAmount%></td>
                                    </tr>
                                </tbody>
                            </table>
                            <div class="col-lg-13">
                            <table class="table table-condensed">
                                <thead>
                                    <tr>
                                        <th class="active" colspan="8">Item Total</th>
                                        <td><%=totalAmount%></td>
                                    </tr>
                                    <tr>                                     
                                        <th class="active" colspan="8">Tax</th>
                                        <td>0.00</td>
                                    </tr>
                                    <tr>                                                                             
                                        <th class="active" colspan="8">Discount</th>
                                        <td>-0.00</td> 
                                    </tr>
                                    <tr>                                                                             
                                        <th class="danger" colspan="8">Order Total</th>
                                        <td><%=po.getCurrency() + " " + totalAmount%></td> 
                                    </tr>
                                </thead>                              
                       
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
                                    
        <% } else { %>

        <%
            Po po = PoManager.retrievePo(anotherPoId);
            String supplierId = po.getSupplierId();
            Supplier supplier = SupplierManager.retrieveSupplier(supplierId);
            String purchaseDate = po.getPurchaseDate();
            String day = purchaseDate.substring(0,2);
            String month = purchaseDate.substring(2,4);
            String year = purchaseDate.substring(4);
            String formatPurchaseDate = day + "/" + month + "/" + year;
            String partPrice = po.getPartPrice();
            double tempPrice = 0;
            try {
                tempPrice = Double.parseDouble(partPrice);
            } catch(NumberFormatException e) { }
        %>        
            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">    
                        <h1 class="panel-title"><i class="icon-file-text-alt"></i> Purchase Order <%=anotherPoId%></h1><br>
                        <a href="index.jsp"><button type="button" class="btn btn-success"><i class="icon-edit"></i> Home</button></a>
                        <a href="#"><button type="button" class="btn btn-default"><i class="icon-print"></i> Print</button></a>
                        <a href="#"><button type="button" class="btn btn-default"><i class="icon-mail-reply"></i> Email</button></a>
                        <a href="updatePurchaseOrder.jsp?poID=<%=anotherPoId%>"><input type="hidden" name="poID" value="<%=anotherPoId%>"><button type="button" class="btn btn-danger"><i class="icon-edit"></i> Edit Order</button></a>
                    </div>
                    
                    <div class="panel-body"> 
                        <div class="table-responsive">
                            <table class="table table-condensed table-hover table-striped tablesorter">                                
                                <thead>
                                    <h1>Purchase Order: <%=anotherPoId%></h1>
                                    <tr>
                                        <th>Supplier</th>
                                        <th>Date </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><address><strong><%=supplier.getSupplierId()%></strong><br>
                                            <%=supplier.getSupplierDescription()%><br>
                                            <%=supplier.getSupplierLocation()%><br></td>
                                        <td><%=formatPurchaseDate%></address></td>
                                    </tr>
                                    <tr>
                                </tbody>
                            </table>
                            <table class="table table-bordered table-hover table-striped tablesorter">
                                <thead  >
                                    <tr class="success">
                                        <th>No</th>
                                        <th>Item ID</th>
                                        <th>Part ID</th>
                                        <th>Quantity</th>
                                        <th>Unit Price</th>
                                        <th>Sub-Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>1</td>
                                        <td><%=po.getItemId()%></td>
                                        <td><%=po.getPartId()%></td>
                                        <td><%=po.getOnOrderQuantity()%></td>
                                        <td><%=po.getPartPrice()%></td>
                                        <td><%=po.getOnOrderQuantity() * tempPrice%></td>
                                    </tr>
                                </tbody>
                            </table>
                            <div class="col-lg-13">
                            <table class="table table-condensed">
                                <thead>
                                    <tr>
                                        <th class="active" colspan="8">Item Total</th>
                                        <td><%=po.getOnOrderQuantity() * tempPrice%></td>
                                    </tr>
                                    <tr>                                     
                                        <th class="active" colspan="8">Tax</th>
                                        <td>0.00</td>
                                    </tr>
                                    <tr>                                                                             
                                        <th class="active" colspan="8">Discount</th>
                                        <td>-0.00</td> 
                                    </tr>
                                    <tr>                                                                             
                                        <th class="danger" colspan="8">Order Total</th>
                                        <td><%=po.getCurrency() + " " + (po.getOnOrderQuantity() * tempPrice)%></td> 
                                    </tr>
                                </thead>                              
                       
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>  
        <% } %>
         
        </div>
    </body>
</html>